#ifndef STEAM_OUT_H
#define STEAM_OUT_H

#include "mytype.h"
#include "str_ext.h"

/*******************************************************************************
 * cpp compatible
 ******************************************************************************/
#ifdef __cplusplus
extern "C"
{
#endif

    /*******************************************************************************
     * @file: steam_out.h
     * @brief: Header file for stream output functions
     ******************************************************************************/
    typedef struct
    {
        int buff_size;                    // Size of the output buffer
        int buff_used;                    // Amount of output buffer used
        char *buff;                       // Pointer to the output buffer
        void (*out)(char *data, int len); // Output function
        int len;
    } stream_out_type;

    /*******************************************************************************
     * @brief: Initialize the stream output structure
     * @param opt: Pointer to the stream_out_type structure
     * @param out_buff: Pointer to the output buffer
     * @param out_buff_size: Size of the output buffer
     * @param out_func: Pointer to the output function
     * @return: None
     * @note: This function initializes the stream output structure with the provided buffer and output function.
     ******************************************************************************/
    void stream_out_init(stream_out_type *opt, char *out_buff, int out_buff_size, void (*out_func)(char *data, int len));

    /*******************************************************************************
     * @brief: Reset the stream output structure
     * @param opt: Pointer to the stream_out_type structure
     * @return: None
     * @note: This function resets the output buffer and file length in the stream output structure.
     ******************************************************************************/
    void stream_out_reset(stream_out_type *opt);

    /*******************************************************************************
     * @brief: Write a formatted number to the output buffer
     * @param opt: Pointer to the stream_out_type structure
     * @param number: Integer number to output
     * @param ipn: Number of decimal places for the value
     * @param prefix: Optional prefix to add before the number
     * @param suffix: Optional suffix to add after the number
     * @return: None
     * @note: This function converts an integer to a string with specified precision and writes it to the output buffer, optionally adding a prefix and suffix.
     ******************************************************************************/
    void stream_out_put_number(stream_out_type *opt, int number, int ipn, const char *prefix, const char *suffix);

    /*******************************************************************************
     * @brief: Write a string to the output buffer
     * @param opt: Pointer to the stream_out_type structure
     * @param data: Pointer to the string to be written
     * @return: None
     * @note: This function writes a string to the output buffer and calls the output function if the buffer is full.
     ******************************************************************************/
    void stream_out_puts(stream_out_type *opt, const char *data);

    /*******************************************************************************
     * @brief: Write data to the output buffer
     * @param opt: Pointer to the stream_out_type structure
     * @param data: Pointer to the data to be written
     * @param len: Length of the data to be written
     * @return: None
     * @note: This function writes data to the output buffer and calls the output function if the buffer is full.
     ******************************************************************************/
    void stream_out_data(stream_out_type *opt, const char *data, int len);

    /*******************************************************************************
     * @brief: End the stream output
     * @param opt: Pointer to the stream_out_type structure
     * @return: None
     * @note: This function outputs any remaining data in the buffer and resets the used size.
     ******************************************************************************/
    void stream_out_end(stream_out_type *opt);

    /*******************************************************************************
     * @brief: Write formatted output to the stream, inner buffer size is 160
     * @param opt: Pointer to the stream_out_type structure
     * @param fmt: Format string
     * @return: None
     * @note: This function formats a string using the provided format and writes it to the output buffer.
     ******************************************************************************/
    void stream_out_fmt(stream_out_type *opt, const char *fmt, ...);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
/*******************************************************************************
 * file end: Header file for stream output functions
 ******************************************************************************/
#endif // STEAM_OUT_H
